package com.sxt.pro.controller;


import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxt.pro.pojo.InclusiveService;
import com.sxt.pro.service.InclusiveServiceService;
import com.sxt.pro.vo.InclusiveServiceVo;
import com.sxt.sys.common.AppFileUtils;
import com.sxt.sys.common.Constast;
import com.sxt.sys.common.DataGridView;
import com.sxt.sys.common.ResultObj;
import com.sxt.sys.common.WebUtils;


/**  

* @Description:  普惠业务控制层

* @author xinxiaokang  

* @date 2021年3月8日 上午10:33:29

*/
@RestController
@RequestMapping("/inclusiveService")
public class InclusiveServiceController {
	
	@Autowired
	private InclusiveServiceService inclusiveServiceService;
	
	/**
	 * 查询普惠业务及分页列表
	 * @param inclusiveServiceVo
	 * @return
	 */
	@RequestMapping("/loadAllInclusiveService")
	public DataGridView loadAllInclusiveService(InclusiveServiceVo inclusiveServiceVo)
	{
		 //存入网点编号和用户名放入session中
		 int banknbr = (int) WebUtils.getSession().getAttribute("banknbr");
		 inclusiveServiceVo.setBanknbr(banknbr);
		//初始化 当前页、每页显示条数
		IPage<InclusiveService> page = new Page<>(inclusiveServiceVo.getPage(), inclusiveServiceVo.getLimit());
		//封装对象查询条件
		QueryWrapper<InclusiveService> queryWrapper = new QueryWrapper<InclusiveService>();
		//筛选条件：网点编号
		queryWrapper.eq(inclusiveServiceVo.getBanknbr()!=null&&inclusiveServiceVo.getBanknbr()!=0,"banknbr",inclusiveServiceVo.getBanknbr());
		//筛选条件:客户类型
		queryWrapper.eq(StringUtils.isNotBlank(inclusiveServiceVo.getCustomertype()), "customertype", inclusiveServiceVo.getCustomertype());
		//筛选条件：贷款类型
		queryWrapper.eq(StringUtils.isNotBlank(inclusiveServiceVo.getLoantype()),"loantype",inclusiveServiceVo.getLoantype());
		//筛选条件：大于开始时间
		queryWrapper.ge(inclusiveServiceVo.getStartTime()!=null, "createtime", inclusiveServiceVo.getStartTime());
		//筛选条件：小于结束时间
		queryWrapper.le(inclusiveServiceVo.getEndTime()!=null, "createtime", inclusiveServiceVo.getEndTime());
		queryWrapper.orderByDesc("createtime");
		//获取分页数据
		this.inclusiveServiceService.page(page, queryWrapper);
		//产品类型循环赋值
//		List<Product> records = page.getRecords();
//		for (Product product : records) {
//			ProductType productType = this.productTypeService.getById(product.getProducttype());
//			if(null!=productType) {
//				product.setProducttypename(productType.getProducttypename());
//			}
//		}
//		return new DataGridView(page.getTotal(), records);
		return new DataGridView(page.getTotal(), page.getRecords());
	}
	
	/**
	 * 添加普惠业务
	 * @param inclusiveServiceVo
	 * @return
	 */
	@RequestMapping("addInclusiveService")
	public ResultObj addInclusiveService(InclusiveServiceVo inclusiveServiceVo) 
	{
		//添加时，网点默认
		int banknbr = (int) WebUtils.getSession().getAttribute("banknbr");
		int available = 0;  //默认未审批
		inclusiveServiceVo.setBanknbr(banknbr);
		inclusiveServiceVo.setAvailable(available);
		try {
			if(inclusiveServiceVo.getTestqrcode()!=null&&inclusiveServiceVo.getTestqrcode().endsWith("_temp")) {
				//测试二维码
				String newName= AppFileUtils.renameFile(inclusiveServiceVo.getTestqrcode());
				inclusiveServiceVo.setTestqrcode(newName);
			}
			if(inclusiveServiceVo.getPayqrcode()!=null&&inclusiveServiceVo.getPayqrcode().endsWith("_temp")) {
				//支付二维码
				String newName=AppFileUtils.renameFile(inclusiveServiceVo.getPayqrcode());
				inclusiveServiceVo.setPayqrcode(newName);
			}
			inclusiveServiceVo.setCreatetime(new Date());
			this.inclusiveServiceService.save(inclusiveServiceVo);
			return ResultObj.ADD_SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ResultObj.ADD_ERROR;
		}
	}

	/**
	 * 修改普惠业务
	 * @param inclusiveServiceVo
	 * @return
	 */
	@RequestMapping("updateInclusiveService")
	public ResultObj updateInclusiveService(InclusiveServiceVo inclusiveServiceVo) 
	{
		try {
			//修改 测试二维码
			if(!(inclusiveServiceVo.getTestqrcode()!=null&&inclusiveServiceVo.getTestqrcode().equals(Constast.IMAGES_DEFAULTGOODSIMG_PNG))) {
				if(inclusiveServiceVo.getTestqrcode().endsWith("_temp")) {
					String newName= AppFileUtils.renameFile(inclusiveServiceVo.getTestqrcode());
					inclusiveServiceVo.setTestqrcode(newName);
					//删除原先的图片
					String oldPath=this.inclusiveServiceService.getById(inclusiveServiceVo.getId()).getTestqrcode();
					if (!(oldPath.equals(Constast.IMAGES_DEFAULTGOODSIMG_PNG))) {
						AppFileUtils.removeFileByPath(oldPath);
					}
				}
			}
			//修改支付二维码
			if(!(inclusiveServiceVo.getPayqrcode()!=null&&inclusiveServiceVo.getPayqrcode().equals(Constast.IMAGES_DEFAULTGOODSIMG_PNG))) {
				if(inclusiveServiceVo.getPayqrcode().endsWith("_temp")) {
					String newName= AppFileUtils.renameFile(inclusiveServiceVo.getPayqrcode());
					inclusiveServiceVo.setPayqrcode(newName);
					//删除原先的图片
					String oldPath=this.inclusiveServiceService.getById(inclusiveServiceVo.getId()).getPayqrcode();
					if (!(oldPath.equals(Constast.IMAGES_DEFAULTGOODSIMG_PNG))) {
						AppFileUtils.removeFileByPath(oldPath);
					} 
				}
			}
			this.inclusiveServiceService.updateById(inclusiveServiceVo);
			return ResultObj.UPDATE_SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ResultObj.UPDATE_ERROR;
		}
	}

	/**
	 * 删除产品
	 * @param id
	 * @param testqrcode
	 * @param payqrcode
	 * @return
	 */
	@RequestMapping("deleteInclusiveService")
	public ResultObj deleteInclusiveService(Integer id,String testqrcode,String payqrcode) {
		try {
			//删除掌中二维码文件
			if (!testqrcode.isEmpty() && !testqrcode.equals("images/defaultgoodsimg.png")) {
				AppFileUtils.removeFileByPath(testqrcode);
			}
			//删除微信二维码文件
			if (!payqrcode.isEmpty() && !payqrcode.equals("images/defaultgoodsimg.png")) {
				AppFileUtils.removeFileByPath(payqrcode);
			}
			this.inclusiveServiceService.removeById(id);
			return ResultObj.DELETE_SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ResultObj.DELETE_ERROR;
		}
	}

	
	/**
	 * 批量审核
	 * @param inclusiveServiceVo
	 * @return
	 */
	@RequestMapping("batchUpdateInclusiveService")
	public ResultObj batchUpdateInclusiveService(InclusiveServiceVo inclusiveServiceVo) {
		int available = 1; //已审批标志
		try {
			for (Integer id : inclusiveServiceVo.getIds()) {
				this.inclusiveServiceService.batchUpdateInclusiveService(available,id);
			}
			return ResultObj.UPDATE_SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ResultObj.UPDATE_ERROR;
		}
	}
	
	
	/**
	 * 批量取消审核
	 * @param inclusiveServiceVo
	 * @return
	 */
	@RequestMapping("batchOffupdateInclusiveService")
	public ResultObj batchOffupdateInclusiveService(InclusiveServiceVo inclusiveServiceVo) {
		int available = 0; //未审批标志
		try {
			for (Integer id : inclusiveServiceVo.getIds()) {
				this.inclusiveServiceService.batchUpdateInclusiveService(available,id);
			}
			return ResultObj.UPDATE_SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ResultObj.UPDATE_ERROR;
		}
	}
	
	
}
